package Power_Asisst.main;

import java.util.ArrayList;
import java.util.List;

import process.CustomArrayAdapter;
import process.SendFileUDP;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListPowerpoint extends Activity {

	ListView lv ;
	AlertDialog.Builder builder;
	AlertDialog alter;
	private String Path = "";
	private String host ="";
	private int port = 0;

	private String[] IP ;
	Untils svUntil = new Untils();
	String[]ipArray;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listpowerpoint);
		lv = (ListView)findViewById(R.id.lv_listpowerpoint);
		builder = new AlertDialog.Builder(this);
		 Bundle bundle = getIntent().getExtras();
		 String ds = bundle.getString("DanhSachFile");
		 IP=bundle.getStringArray("IP");
	 
		// host = bundle.getString("IP");
		 port = bundle.getInt("Port");
		 String[]list = ds.split(";");
		 List<String>dsfile = new ArrayList<String>();
		 for(int i=0;i<list.length;i++)
		 {
			 dsfile.add(list[i]);
		 }
		 if(dsfile.size()>0)
		 {
			 lv.setAdapter(new CustomArrayAdapter(this,
					android.R.layout.simple_expandable_list_item_2, dsfile));
			 lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					
					Object o = lv.getItemAtPosition(arg2);
					Path = o.toString();
					builder.setMessage("Bạn có muốn mở file "+ Path + " không???");
					builder.setCancelable(false);
					builder.setPositiveButton("Yes", new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							for(int i=0;i<IP.length;i++)
							{
								String hosts = IP[i];
								SendFileUDP u = new SendFileUDP();
								u.sendThongTin(hosts, port, Path);
								Intent intent1 = new Intent();
								intent1.setClass(getBaseContext(),RemotePowerPoint.class);
						    	intent1.putExtra("IP", IP);
						    	intent1.putExtra("Giao", "UDP");
						    	startActivity(intent1);
							}
					    	
						}
					});
					
					builder.setNegativeButton("No", new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.cancel();
						}
					});
					alter = builder.create();
					alter.show();
				}
			});
		 }
	}
}
